<template>
  <div class="buttonAddExiteee">
    <i @click='dialogOpen' class="iconfont icon-bianji1"></i>
    <!--<el-button type="text" size="mini" @click="dialogOpen">管理 </el-button>-->
    <el-dialog :close-on-click-modal="false" :append-to-body="true"  title="管理任务" :visible.sync="dialogFormVisibleAdd" @opened="$store.commit('dialogTop')" @close="resetForm('ruleFormAdd')">
      <el-form :model="ruleFormAdd" :rules="rulesAdd" ref="ruleFormAdd" label-width="90px" class="demo-ruleForm">
        <el-form-item label="文件类型:" prop="fileType" >
          <el-select v-model="ruleFormAdd.fileType" @change="getFname(ruleFormAdd.fileType)" style="width: 100%;" size="mini"  placeholder="全部">
            <el-option
              v-for="item in options"
              :key="item.fileType"
              :label="item.fileName"
              :value="item.fileType">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件名称:" prop="fileName">
          <span style="color: #3975FE;cursor: pointer;" @click="downLoadXml(ruleFormAdd.downloadPath)">
            {{ruleFormAdd.fileName}}
          </span>

        </el-form-item>
      </el-form>
      <div style="text-align: center;margin: 20px 0 1px 0;">
        <el-button v-show="ruleFormAdd.fileName" v-if="$store.state.btnMenuIds.indexOf(198)>-1"  type="primary" size="small" @click="handleDelete(ruleFormAdd.fileName)">删 除</el-button>
        <el-button type="primary" v-if="$store.state.btnMenuIds.indexOf(197)>-1" size="small" @click="upload()">上 传</el-button>
      </div>
    </el-dialog>
    <auploadDialog ref="auploadDialog" @getParrowsInfo="getParrowsInfo"  :enterpriseNo="enterpriseNo" :fileType="fileType"></auploadDialog>
  </div>
</template>

<script>
  import auploadDialog from "./uploadDialog.vue"
  export default {
    props:["infoList","enterpriseNo"],
    data () {
      return {
        options: [
          {fileName:"提前30日书面通知",fileType:6,name:""},
          {fileName:"社会公示",fileType:7,name:""},
          {fileName:"押金退还",fileType:8,name:""},
          {fileName:"车辆及相关设施清退",fileType:9,name:""},
          {fileName:"车辆备案注销",fileType:10,name:""},
        ],

        ruleFormAdd: {
          "fileName": "",
          "fileType": "",
          "downloadPath": "",


        },
        fileType: "",
        dialogFormVisibleAdd: false,
        rulesAdd: {

        }
      }
    },
    created(){

    },
    components: {auploadDialog},
    methods: {
      //下载模板
      downLoadXml(url){
        window.location.href = url
      },
      upload(){
        this.fileType=this.ruleFormAdd.fileType
       this.$refs.auploadDialog.dialogUloadAdd=true
      },
      handleDelete(name){
        this.$confirm('确认删除文件 “' + name + '” , 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(process.env.API_HOST + `/enterprise/deleteEnterpriseFile?`,{"enterpriseNo":this.enterpriseNo,
            "fileAttribute":1,
            "fileType":this.ruleFormAdd.fileType})
            .then(response => {
              const result = response.data
              if (result.code === 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.options= [
                  {fileName:"提前30日书面通知",fileType:6,name:""},
                  {fileName:"社会公示",fileType:7,name:""},
                  {fileName:"押金退还",fileType:8,name:""},
                  {fileName:"车辆及相关设施清退",fileType:9,name:""},
                  {fileName:"车辆备案注销",fileType:10,name:""},
                ],
                this.dialogFormVisibleAdd= false
                this.$emit("getrowsInfo")
              }
            })
        }).catch(() => {

        });
      },
      getFname(fileType){

        this.options.forEach(item=>{
          if(item.fileType==fileType){
            this.ruleFormAdd.fileName=item.name
            this.ruleFormAdd.downloadPath=item.downloadPath
            this.ruleFormAdd.fileType=item.fileType
          }
        })
      },
      dialogOpen(){
        this.dialogFormVisibleAdd=true
        let isPass=true
        this.infoList.forEach((item)=>{
          this.options.forEach(item1=>{
            if(item.fileType==item1.fileType){
              item1.name=item.fileName
              item1.downloadPath=item.downloadPath
            }
          })
          if(item.fileType&&isPass){
            isPass=false
            this.ruleFormAdd.fileType=item.fileType
            this.ruleFormAdd.fileName=item.fileName
            this.ruleFormAdd.downloadPath=item.downloadPath
          }
        })
      },
      //提交回调
      handleDiaSureAdd(){
        this.axios.post(process.env.API_HOST + '/systemUser/insert', {...this.ruleFormAdd})
          .then(response => {
            let result=response.data
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '新增成功!'
              });
              this.dialogFormVisibleAdd = false
              this.$emit('getListInfo')
              this.resetForm('ruleFormAdd')
            }

          })
          .catch(error => {
            console.log(error);
          });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
//            this.handleDiaSureAdd()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      getParrowsInfo(){
        this.$emit("getrowsInfo")
        setTimeout(()=>{
          this.dialogOpen()
        },200)
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.$emit("getrowsInfo")
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" >
  @import "../../../common/stylus/mixin.styl"
  .buttonAddExiteee
    display inline-block

</style>
